﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace PathTransfer
{
    /*
     * <?xml version="1.0" encoding="utf-8"?>
<Appdomain>
  <AppInfo CustomAppName="QQ" EXEAppName="QQ.exe" IconName="QQ.png" GroupName="Common" MyTips="腾讯的聊天软件" MultiVerson="false" Architecture="false"></AppInfo>
  <AppInfo CustomAppName="Origin" EXEAppName="Origin.exe" IconName="XX.png" GroupName="Specific" MyTips="未知名的软件" MultiVerson="true" Architecture="false"></AppInfo>
</Appdomain>
     */

    public class AppInfo:IEquatable<AppInfo>
    {
        public AppInfo(string customAppName,
            string exeAppName,
            string iconName,
            string fullPath,
            string groupName,
            string Tips,
            bool MultiVerson,
            bool Architecture)
        {
            this.CustomAppName = customAppName;
            this.EXEAppName = exeAppName;
            this.IconName = iconName;
            this.FullPath = fullPath;
            this.GroupName = groupName;
            this.Tips = Tips;
            this.MultiVerson = MultiVerson;
            this.Architecture = Architecture;
        }

        public string CustomAppName;
        public string EXEAppName;
        public string IconName;
        public string FullPath;
        public string GroupName;
        public string Tips;
        public bool MultiVerson;
        public bool Architecture;

        public bool Equals(AppInfo other)
        {
            return this.CustomAppName == other.CustomAppName;
        }

        public override int GetHashCode()
        {
            return CustomAppName.GetHashCode();
        }  
    }

    class XmlHelper
    {
        const string compareXmlPath = "./CustomAppInfo.xml";
        const string xmlGeneredPath = "./Fullpath.xml";

        private static XmlHelper instane;
        public static XmlHelper _Instance
        {
            get
            {
                if (instane == null)
                {
                    instane = new XmlHelper();
                    return instane;
                }
                return instane;
            }
        }

        public List<AppInfo> AllCustomInfo = new List<AppInfo>();

        private XmlDocument ComPareXmlDoc = new XmlDocument();
        private XmlNode CompareRootNode;

        public XmlHelper()
        {
            if (!File.Exists(compareXmlPath)) { MessageBox.Show("自定义对照的xml没有找到！"); return; }

            try
            {
                ComPareXmlDoc.Load(compareXmlPath);
                XmlLoad();
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
                return;
            }
            
        }

        /// <summary>
        /// 加载用于对照的自定义xml
        /// </summary>
        public void XmlLoad()
        {
            CompareRootNode = ComPareXmlDoc.SelectSingleNode("Appdomain");
            XmlNodeList nodeList = CompareRootNode.ChildNodes;

            if (nodeList.Count <= 0) return;

            foreach (XmlNode node in nodeList)
            {
                XmlElement e = node as XmlElement;
                string cusAppName = e.GetAttribute("CustomAppName");
                string exeAppName = e.GetAttribute("EXEAppName");
                string iconName = e.GetAttribute("IconName");
                string goupName = e.GetAttribute("GroupName");
                string tips = e.GetAttribute("MyTips");
                bool multiVersion = bool.Parse(e.GetAttribute("MultiVerson"));
                bool architecture = bool.Parse(e.GetAttribute("Architecture"));
                AppInfo appInfo = new AppInfo(cusAppName, exeAppName, iconName, "", goupName, tips, multiVersion, architecture);

                if (AllCustomInfo == null || AllCustomInfo.Contains(appInfo)) break;

                AllCustomInfo.Add(appInfo);
            }

            return;
        }

        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name=""></param>
        public void AddXmlNode(List<AppInfo> AppsInfo)
        {
            if (AppsInfo == null || AppsInfo.Count == 0) return;

            File.Delete(xmlGeneredPath);

            //没有就创建新的xml文档
            if (!File.Exists(xmlGeneredPath))
            {
                XmlDocument doc = new XmlDocument();
                XmlElement newRootNode = doc.CreateElement("Appdomain");
                doc.AppendChild(newRootNode);
                doc.Save(xmlGeneredPath);
            }

            //添加新节点
            XmlDocument generd = new XmlDocument();
            generd.Load(xmlGeneredPath);
            XmlNode rootNode = generd.SelectSingleNode("Appdomain");

            foreach (AppInfo info in AppsInfo)
            {
                XmlElement appInfo = generd.CreateElement("AppInfo");
                appInfo.SetAttribute("CustomAppName", info.CustomAppName);
                appInfo.SetAttribute("EXEAppName", info.EXEAppName);
                appInfo.SetAttribute("IconName", info.IconName);
                appInfo.SetAttribute("GroupName", info.GroupName);
                appInfo.InnerText = info.FullPath;
                appInfo.SetAttribute("MyTips", info.Tips);

                rootNode.AppendChild(appInfo);
            }
            generd.AppendChild(rootNode);
            generd.Save(xmlGeneredPath);


        }
    }
}
